package ${package}.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import ${package}.constant.CommonConstant;
import ${package}.dto.${className}Dto;
import ${package}.mapper.${className}Mapper;
import ${package}.service.${className}Service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ${tableComment}业务层实现类
 * 
 * @author ${author}
 * @date ${date}
 */
@Service
public class ${className}ServiceImpl implements ${className}Service {
	@Autowired
	private ${className}Mapper ${changeClassName}Mapper;

	private static final Logger logger = LoggerFactory.getLogger(${className}ServiceImpl.class);

	@Override
	@SuppressWarnings("resource")
	public String select${className}List(${className}Dto dto) {
		JSONObject json = new JSONObject();

		try {
			PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize(), true);
			// 调用${tableComment}持久层获得数据
			List<${className}Dto> result = ${changeClassName}Mapper.select${className}List(dto);
			// 分页插件调用
			Page<${className}Dto> page = (Page<${className}Dto>) result;
			json.put("data", page.getResult());
			json.put("total", page.getTotal());
			json.put("status", "Y");
		} catch (Exception e) {
			logger.error("查询${tableComment} 失败: {}", e.getMessage(), e);
			json.put("message", e.getMessage());
			json.put("status", "N");
		}
		return json.toJSONString();
	}

	@Override
	public String update${className}ById(${className}Dto dto) {
		JSONObject json = new JSONObject();

		try {
			// 调用${tableComment}持久层方法更新数据
			int result = ${changeClassName}Mapper.update${className}ById(dto);
			// 判断返回值
			if (result > 0) {
				json.put("message", "更新成功");
				json.put("status", "Y");
			} else {
				json.put("message", "更新失败");
				json.put("status", "N");
			}
		} catch (Exception e) {
			logger.error("update${className}ById 失败: ", e.getMessage(), e);
			json.put("message", "更新失败" + e.getMessage());
			json.put("status", "N");
		}
		return json.toJSONString();
	}

	@Override
	public String save${className}Dto(${className}Dto dto) {
		JSONObject json = new JSONObject();

		try {
			// 验证id是否存在 ，存在id 更新数据，不存在新增数据
			int result = null != dto.getId() ? ${changeClassName}Mapper.update${className}ById(dto)
					: ${changeClassName}Mapper.save${className}Dto(dto);
			// 判断返回值
			if (result > 0) {
				json.put("message", null != dto.getId() ? "更新成功" : "保存成功");
				json.put("status", "Y");
			} else {
				json.put("message", null != dto.getId() ? "更新失败" : "保存失败");
				json.put("status", "N");
			}
		} catch (Exception e) {
			logger.error("save${className}Dto 失败: ", e.getMessage(), e);
			json.put("message", null != dto.getId() ? "更新失败" + e.getMessage() : "保存失败" + e.getMessage());
			json.put("status", "N");
		}
		return json.toJSONString();
	}

	@Override
	public String select${className}ById(${className}Dto dto) {
		JSONObject json = new JSONObject();

		try {
			// 调用${tableComment}持久层获得当前ID数据
			${className}Dto result = ${changeClassName}Mapper.select${className}ById(dto);
			// easyui GridDataModel 赋值
			json.put("data", result);
			json.put("status", "Y");
		} catch (Exception e) {
			logger.error("查询${tableComment} 当前数据ID失败: {}", e.getMessage(), e);
			json.put("message", e.getMessage());
			json.put("status", "N");
		}
		return json.toJSONString();
	}


	@Override
	public String cancel${className}ById(${className}Dto dto) {
		JSONObject json = new JSONObject();

		try {
			// 作废数据根据ID
			int result = ${changeClassName}Mapper.cancel${className}ById(dto);
			// 判断返回值
			if (result > 0) {
				json.put("message", "作废成功");
				json.put("status", "Y");
			} else {
				json.put("message", "作废失败");
				json.put("status", "N");
			}
		} catch (Exception e) {
			logger.error("save${className}Dto 失败: ", e.getMessage(), e);
			json.put("message", "作废失败" + e.getMessage());
			json.put("status", "N");
		}
		return json.toJSONString();
	}
	
}